package team02.server.bean;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import team02.client.core.test.question.QuestionS;
import team02.server.model.MySQLModel;

public class MarkBean extends MySQLModel {
	private int examineeId;
	private QuestionBean question;
	private String answer;
	private int score;
	List<MarkBean> listmarkbean;
	public MarkBean() {
		super(null);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int load() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int save() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
	public QuestionBean getQuestion() {
		return question;
	}

	public void setQuestion(QuestionBean question) {
		this.question = question;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}
	//Lay danh sach cau hoi, va cau tra loi cua thi sinh
	public List<MarkBean> listQuestionAnswer(int testId,int examineeId) throws SQLException
	{
		List<MarkBean> listQuestionAnswer=new LinkedList<MarkBean>();
		List<QuestionBean> listQues=new QuestionBean().listAllQuestionByTestId(testId,examineeId);
		for (QuestionBean ques : listQues) {
			MarkBean m=new MarkBean();
			m.question=ques;
			m.examineeId=examineeId;
			listQuestionAnswer.add(m);
		}
		this.listmarkbean=listQuestionAnswer;
		return listQuestionAnswer;
	}
	//lay danh sach cau tra loi doi voi cau hoi trac nghiem
	public String getExamineeSelection(){
		String answerString="";
		ArrayList<Integer> list=question.getExamineeSelection();
		for (Integer i : list) {
			answerString+=i.toString()+", ";
			}
		return answerString;
	}
	//lay cau tra loi voi cau hoi tu luan
	public ArrayList<String> getExamineeWrite()
	{
		ArrayList<String> list=question.getExamineeWrite();
		return list;
	}
	public String getMarkName()
	{
		return this.question.getId()+"_mark";
	}
	public String getCmtName()
	{
		return this.question.getId()+"_cmt";
	}
	//cham diem bang tay
	public int update(Hashtable<String, String> htbl) throws SQLException{
		int score2=0;
		int exId=Integer.parseInt(htbl.get("examinee_id"));
		htbl.remove("examinee_id");
		htbl.remove("selected_id");
		htbl.remove("save");
		for (MarkBean b : listmarkbean) {
			int newscore=0;
			String cmt="";
			int status=0;
			Enumeration e=htbl.keys();
			while(e.hasMoreElements())
			{
				String key1=e.nextElement().toString();
				if(key1.equals(b.getMarkName()))
					{
					newscore=Integer.parseInt( htbl.get(key1));
					htbl.remove(key1);
					status=1;
					}
				if(key1.equals(b.getCmtName())){
					cmt=htbl.get(key1);
					htbl.remove(key1);
				}
			}
			if(status==1)
				{
				String query="UPDATE jp_answer SET score='"+newscore+"', ins_content='"+cmt+"' WHERE question_id='"+b.question.getId()+"' AND examinee_id='"+b.examineeId+"'";
				initConnection();
				executeUpdate(query);
				query="UPDATE jp_examinee SET marked=1 WHERE id='"+b.examineeId+"'";
				executeUpdate(query);
				score2+=newscore;
				}
			else {
				score2+=b.question.getScore();
			}
			//if(htbl.isEmpty())break;
		}
		String query="UPDATE jp_examinee SET score2='"+score2+"' WHERE id='"+exId+"'";
		System.out.println(query);
		initConnection();
		executeUpdate(query);
		return 0;
	}

	public int getExamineeId() {
		return examineeId;
	}

	public void setExamineeId(int examineeId) {
		this.examineeId = examineeId;
	}

}
